import { defineStore } from 'pinia';
import axios from '../utils/axios';

export const useArticleStore = defineStore('article', {
  state: () => ({
    articles: [],
    currentArticle: null,
    loading: false,
    error: null
  }),

  getters: {
    allArticles: (state) => state.articles,
    articleById: (state) => (id) => state.articles.find(article => article.id === id)
  },

  actions: {
    async fetchArticles() {
      this.loading = true;
      try {
        const response = await axios.get('/articles');
        this.articles = response;
        this.error = null;
      } catch (error) {
        console.error('Failed to fetch articles:', error);
        this.error = 'Failed to fetch articles';
      } finally {
        this.loading = false;
      }
    },

    async fetchArticleById(id) {
      this.loading = true;
      try {
        const response = await axios.get(`/articles/${id}`);
        this.currentArticle = response;
        this.error = null;
        return response;
      } catch (error) {
        console.error('Failed to fetch article:', error);
        this.error = 'Failed to fetch article';
      } finally {
        this.loading = false;
      }
    },

    async createArticle(articleData) {
      try {
        const response = await axios.post('/articles', articleData);
        this.articles.push(response);
        return response;
      } catch (error) {
        console.error('Failed to create article:', error);
        this.error = 'Failed to create article';
        throw error;
      }
    },

    async updateArticle(id, articleData) {
      try {
        const response = await axios.put(`/articles/${id}`, articleData);
        const index = this.articles.findIndex(article => article.id === id);
        if (index !== -1) {
          this.articles[index] = response;
        }
        if (this.currentArticle && this.currentArticle.id === id) {
          this.currentArticle = response;
        }
        return response;
      } catch (error) {
        console.error('Failed to update article:', error);
        this.error = 'Failed to update article';
        throw error;
      }
    },

    async deleteArticle(id) {
      try {
        await axios.delete(`/articles/${id}`);
        this.articles = this.articles.filter(article => article.id !== id);
        if (this.currentArticle && this.currentArticle.id === id) {
          this.currentArticle = null;
        }
      } catch (error) {
        console.error('Failed to delete article:', error);
        this.error = 'Failed to delete article';
        throw error;
      }
    },

    async likeArticle(id) {
      try {
        const response = await axios.post(`/articles/${id}/like`);
        const index = this.articles.findIndex(article => article.id === id);
        if (index !== -1) {
          this.articles[index].likeCount = response.likeCount;
        }
        if (this.currentArticle && this.currentArticle.id === id) {
          this.currentArticle.likeCount = response.likeCount;
        }
        return response;
      } catch (error) {
        console.error('Failed to like article:', error);
        this.error = 'Failed to like article';
        throw error;
      }
    },

    async searchArticles(keyword) {
      this.loading = true;
      try {
        const response = await axios.get(`/articles/search?keyword=${keyword}`);
        this.articles = response;
        this.error = null;
      } catch (error) {
        console.error('Failed to search articles:', error);
        this.error = 'Failed to search articles';
      } finally {
        this.loading = false;
      }
    }
  }
});